package cn.jbolt.ec.member;

import cn.jbolt.base.JBoltBaseController;
import cn.jbolt.common.model.*;
import cn.jbolt.ec.task.OrderSchedule;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;

/**
 * @author xieguo
 * @description 会员控制层
 */
public class EcMemberController extends JBoltBaseController {
    @Inject
    AddressService addressService;
    @Inject
    CouponService couponService;
    @Inject
    MemberService memberService;
    @Inject
    RetailerMemberBalanceService retailerMemberBalance;


    /**
     * 会员地址信息
     *
     * @param openSession
     */
    public void get_member_address_info(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            JSONObject jo1 = new JSONObject();
            try {
                jo1.put("code", 0);
                jo1.put("data", addressService.get_member_address_info(getSessionAttr(openSession+"_member_id")));
                renderJson(jo1);
            } catch (NullPointerException e) {
                jo1.put("code", 1);
                jo1.put("msg", "您的信息已失效请重新关注公众号");
                renderJson(jo1);
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }
    }

    /**
     * 保存会员地址
     *
     * @param adress
     * @param adress_detail
     * @param mobile
     * @param consignee
     * @param is_major
     * @param province
     * @param city
     * @param area
     */
    public void address_append(String openSession, String adress, String adress_detail, String mobile, String consignee
            , Integer is_major, String province, String city, String area) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            JSONObject jo1 = new JSONObject();
            if (addressService.address_append(getSessionAttr(openSession+"_member_id"), adress
                    , adress_detail, mobile, consignee, is_major, province, city, area)) {
                jo1.put("code", 0);
                jo1.put("msg", "保存成功");
                renderJson(jo1);
            } else {
                jo1.put("code", 1);
                jo1.put("msg", "您的信息已失效请重新关注公众号");
                renderJson(jo1);
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 更新会员地址
     *
     * @param id
     * @param adress
     * @param adress_detail
     * @param mobile
     * @param consignee
     * @param is_major
     * @param province
     * @param city
     * @param area
     */
    public void address_modify(Integer id, String openSession, String adress, String adress_detail, String mobile, String consignee
            , Integer is_major, String province, String city, String area) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            JSONObject jo1 = new JSONObject();
            if (addressService.address_modify(id, getSessionAttr(openSession+"_member_id"), adress
                    , adress_detail, mobile, consignee, is_major, province, city, area)) {
                jo1.put("code", 0);
                jo1.put("msg", "更新成功");
                renderJson(jo1);
            } else {
                jo1.put("code", 1);
                jo1.put("msg", "您的信息已失效请重新关注公众号");
                renderJson(jo1);
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 删除会员地址
     * @param id
     */
    public void address_delete(Integer id,String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            JSONObject jo1 = new JSONObject();
            if (addressService.address_delete(id)) {
                jo1.put("code", 0);
                jo1.put("msg", "删除成功");
                renderJson(jo1);
            } else {
                jo1.put("code", 1);
                jo1.put("msg", "您的信息已失效请重新关注公众号");
                renderJson(jo1);
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 获取商家优惠券
     * @param openSession
     */
    public void get_member_coupon(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(couponService.get_member_coupon(getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 获取分销商积分
     * @param openSession
     */
    public void get_spreader_credit(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(memberService.get_spreader_credit(getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }


    /**
     * 获取会员平台余额
     * @param openSession
     */
    public void get_platform_balance(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(memberService.get_platform_balance(getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }


    /**
     * 获取会员商家余额
     * @param openSession
     */
    public void get_member_retailer_balance(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(retailerMemberBalance.get_member_retailer_balance(getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 获取会员信息
     * @param openSession
     */
    public void get_info(String openSession) {

        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(memberService.get_info(getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 会员信息修改
     *
     * @param nickname
     * @param mobile
     * @param wechat_no
     * @param qq
     * @param openSession
     */
    public void modify_info(String nickname, String mobile, String wechat_no, String qq,String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            JSONObject jo1 = new JSONObject();
            if (memberService.modify_info(getSessionAttr(openSession+"_member_id"), nickname, mobile, wechat_no, qq)) {
                jo1.put("code", 0);
                jo1.put("msg", "修改成功");
                renderJson(jo1);
            } else {
                jo1.put("code", 1);
                jo1.put("msg", "您的信息已失效，请重新关注公众号");
                renderJson(jo1);
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 会员商家优惠券
     * @param retailer_id
     * @param openSession
     */
    public void get_coupon_by_retailer(int retailer_id,String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(memberService.get_coupon(retailer_id, getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 获取商家余额
     * @param retailer_id
     * @param openSession
     */
    public void get_balance_by_retailer(int retailer_id, String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            renderJson(memberService.get_retailer_balance(retailer_id, getSessionAttr(openSession+"_member_id")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    /**
     * 判断会员是否为分销商
     *
     */
    public void judge_spreader(String openSession) {
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            Integer member_id=getSessionAttr(openSession+"_member_id");
            BecSpreader spreader= new BecSpreader().dao().findFirst("select member_id,audited from bec_spreader where member_id=?",member_id);
            if (null==spreader){
                jo.put("ok",0);
                jo.put("msg","此用户不是分销商");
                renderJson(jo);
            }else {
                Integer a=getSessionAttr(openSession+"_spreader_judge");
                Integer b=spreader.getAudited();
                if (a.equals(b)){
                    jo.put("ok",0);
                    jo.put("msg","正常");
                    renderJson(jo);
                }else {
                    jo.put("ok",1001);
                    jo.put("msg","信息过期，请重新获取");
                    renderJson(jo);
                }
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }

    public void judge_retailer_staff(String openSession){

        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_member_id")){
            Integer member_id=getSessionAttr(openSession+"_member_id");
            BecRetailerStaff retailerStaff=new BecRetailerStaff();
            BecRetailerStaff becRetailerStaff=retailerStaff.findById(member_id);
            if (null==becRetailerStaff){
                jo.put("ok",1);
                jo.put("msg","此用户不是商家员工");
                renderJson(jo);
            }else {
                if (becRetailerStaff.getIsDel()){
                    jo.put("ok",1);
                    jo.put("msg","此用户已经不是商家员工了");
                    renderJson(jo);
                }else {
                    Integer retailer_id=getSessionAttr(openSession+"_retailer_id");

                    if (retailer_id.equals(becRetailerStaff.getRetailerId())){
                        jo.put("ok",0);
                        jo.put("msg","正常");
                        renderJson(jo);
                    }else {
                        jo.put("ok",1001);
                        jo.put("msg","此用户员工信息已过期");
                        renderJson(jo);
                    }

                }
            }
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }
    }

}
